package com.lfk.justweengine.Engine;

import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.IOException;

/**
 * 封装图片的获取
 * Created by liufengkai on 15/11/27.
 */
public class GameTexture {
    private Context e_context;
    private PixelMap e_bitmap;

    public GameTexture(Context e_context) {
        this.e_context = e_context;
        e_bitmap = null;
    }

    public PixelMap getBitmap() {
        return e_bitmap;
    }

    /**
     * get bitmap form assets
     *
     * @param filename
     * @return
     */
    public boolean loadFromAsset(String filename) {
        RawFileEntry rawFileEntry = e_context.getResourceManager().getRawFileEntry(filename);
        try {
            Resource rs = rawFileEntry.openRawFile();
            int length = rs.available();
            byte[] buffer = new byte[length];
            rs.read(buffer,0,length);
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/png";

            ImageSource imageSourceNoOptions = ImageSource.create(buffer, srcOpts);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            e_bitmap = imageSourceNoOptions.createPixelmap(decodingOptions);
            imageSourceNoOptions.release();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

    /**
     * get bitmap form assets
     *
     * @param filename
     * @return
     */
    public boolean loadShootFromAsset(String filename) {
        RawFileEntry rawFileEntry = e_context.getResourceManager().getRawFileEntry(filename);
        try {
            Resource rs = rawFileEntry.openRawFile();
            int length = rs.available();
            byte[] buffer = new byte[length];
            rs.read(buffer,0,length);
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/png";

            ImageSource imageSourceNoOptions = ImageSource.create(buffer, srcOpts);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            PixelMap s_bitmap = imageSourceNoOptions.createPixelmap(decodingOptions);
            imageSourceNoOptions.release();
            ohos.media.image.common.Rect rr = new ohos.media.image.common.Rect(0,100,100,124);
            PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
            options.pixelFormat = PixelFormat.ARGB_8888;
            options.size = new Size(100, 124);
            e_bitmap = PixelMap.create(s_bitmap, rr ,options);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * get bitmap from a big bitmap
     *
     * @param filename
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     */
    public boolean loadFromAssetStripFrame(String filename,
                                           int x, int y,
                                           int width, int height) {
        {

            PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
            options.pixelFormat = PixelFormat.ARGB_8888;
            ImageSource imageSourceNoOptions = ImageSource.create(filename, null);
            PixelMap temp = imageSourceNoOptions.createPixelmap(null);
            Rect rect = new Rect(x, y, width, height);
            e_bitmap = PixelMap.create(temp, rect, options);

            return true;
        }
    }

    public static PixelMap loadFromBigBitmap(GameTexture texture, int x, int y
            , int w, int h) {
        Rect rect = new Rect(x, y, w, h);
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.pixelFormat = PixelFormat.ARGB_8888;
        return PixelMap.create(texture.getBitmap(), rect, options);
    }

    public void setBitmap(PixelMap e_bitmap) {
        this.e_bitmap = e_bitmap;
    }
}
